package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
)

var PORT string

type Site struct {
	Key         string `json:"key"`
	Name        string `json:"name"`
	Type        int    `json:"type"`
	Api         string `json:"api"`
	PlayerType  int    `json:"playerType"`
	Searchable  int    `json:"searchable"`
	QuickSearch int    `json:"quickSearch"`
	Filterable  int    `json:"filterable"`
	Ext         string `json:"ext"`
}

func R_File(path string) string {
	data, _ := os.ReadFile(path)
	return string(data)

}

func CheckFile(path string) bool {
	_, e := os.Stat(path)
	if e == nil {
		return true

	}

	return false
}
func R_Data(j_str string) string {
	js_dir := gjson.Get(j_str, "js_dir").String()
	if !CheckFile(js_dir) {
		return js_dir + "不存在"
	}
	sites := []Site{}
	u := gjson.Get(j_str, "js_host").String() + ":" + gjson.Get(j_str, "js_port").String()
	entries, _ := os.ReadDir(js_dir)
	for _, entry := range entries {
		// js_dir_r := strings.ReplaceAll(js_dir, "./", "")
		// js_dir_r := js_dir[strings.LastIndex(js_dir, "/")+1:]
		if !entry.IsDir() && filepath.Ext(entry.Name()) == ".js" {
			if entry.Name() == "drpy2.min.js" {
				continue
			}
			s_u := u + "/drpy_js/" + entry.Name()
			s := Site{}
			s.Name = strings.ReplaceAll(entry.Name(), ".js", "")
			s.Key = strings.ReplaceAll(entry.Name(), ".js", "")
			s.Type = 3
			s.Api = gjson.Get(j_str, "drpy2_min_js").String()
			if s.Api == "" {
				s.Api = u + "/drpy_js/drpy2.min.js"
			}
			s.PlayerType = 1
			s.Searchable = 1
			s.QuickSearch = 1
			s.Filterable = 1
			s.Ext = s_u

			sites = append(sites, s)
		}
	}
	result := `{}`
	result, _ = sjson.SetRaw(result, "wallpaper", `"`+gjson.Get(j_str, "spdir_muban.wallpaper").String()+`"`)
	result, _ = sjson.SetRaw(result, "spider", `"`+gjson.Get(j_str, "spdir_muban.spider").String()+`"`)
	result, _ = sjson.SetRaw(result, "spider", `"`+gjson.Get(j_str, "spdir_muban.spider").String()+`"`)
	result, _ = sjson.Set(result, "sites", sites)

	return result

}
func main() {
	PORT = "8080"

	// 创建一个默认的路由引擎
	r := gin.Default()

	// GET：请求方式；/hello：请求的路径
	// 当客户端以GET方法请求/hello路径时，会执行后面的匿名函数
	b := CheckFile("./setting.json")
	if b == false {
		fmt.Println(`  {
  "message": "setting.json不存在!",
  "setting.json": {
  "js_dir": "./drpy_js/",
  "spdir_muban": {
  "wallpaper": "",
  "spider": "",
  "lives": ""
  },
  "null_data": 10,
  "js_host": "http://localhost",
  "js_port": "14122"
  }
  }`)
	}
	PORT = gjson.Get(R_File("./setting.json"), "js_port").String()

	r.GET("/drpy", func(c *gin.Context) {
		// c.JSON：返回JSON格式的数据

		c.String(200, R_Data(R_File("./setting.json")))

	})
	r.GET("drpy_js/*p", func(c *gin.Context) {
		fmt.Println("drpy_js")
		p := c.Param("p")
		File_Path := gjson.Get(R_File("./setting.json"), "js_dir").String()
		c.File(File_Path + "/" + p)
	})
	r.GET("video/*p", func(c *gin.Context) {
		fmt.Println("video")
		p := c.Param("p")
		Video_Path := gjson.Get(R_File("./setting.json"), "video_dir").String()
		c.File(Video_Path + "/" + p)
	})
	r.GET("pic/*p", func(c *gin.Context) {
		fmt.Println("pic")
		p := c.Param("p")
		Video_Path := gjson.Get(R_File("./setting.json"), "pic_dir").String()
		c.File(Video_Path + "/" + p)
	})

	// 启动HTTP服务，默认在0.0.0.0:8080启动服务
	r.Run(":" + PORT)
}
